package com.dingwen.pro.demo04;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理调用处理程序
 *
 * @author dingwen
 * @date 2021/10/06
 */
public class ProxyInvocationHandler implements InvocationHandler {
    /**
     * 目标对象
     */
    private Object target;

    public void setTarget(Object target) {
        this.target = target;
    }


    /**
     * 获得代理对象
     *
     * @return {@link Object}
     */
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = method.invoke(target, args);
        log(method.getName());
        return result;
    }


    public void log(String msg){
        System.out.println("日志："+ msg);
    }
}
